<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class CarModelObject extends Model
{

    /**
     * 关联到模型排量的数据表
     *
     * @var string
     */
    protected $table = 'car_model_object2';

    public static function modelObjectByDisplacement( $factory_name, $series_name, $displacement ){

        $sql = "select * from car_model cm left join car_model_object2 cmo2 on cm.id=cmo2.model_id "
            . " where cm.factory_name=? and cm.name=? and cmo2.engine_displacement=?";
        $data = DB::select( $sql, [ $factory_name, $series_name, $displacement ] );
        return $data;
    }

    public static function infoByTid( $tid ){
        $sql = "select * from car_model_object2 where tid=?";
        $data = DB::select( $sql, [ $tid ] );
        return !empty( $data ) ? $data[0] : [];
    }

    public static function oil_transmission( $tid ){

        $sql_transmission = ' select * from model_oil_transmission where model_object_id=?';
        $oil_transmission = DB::select( $sql_transmission, [ $tid ] );

        $source_oil = !empty( $oil_transmission ) ? $oil_transmission[0] : '';

        // 康普顿的油 替换成 科发的油
        if( !empty( $source_oil ) ){
            $oil = KefaOilTransmission::where('transmission_name', $source_oil->name )->firstOrFail();
            if( !empty( $oil ) ){
                $source_oil->name = $oil->name;
                //todo 图片链接属性需要添加
                $source_oil->image = $oil->img_url;
            }

        }
        return $source_oil;
    }

    public static function oil_gt( $tid ){

        $sql_gt  = 'select * from model_oil_engine_gt where model_object_id=?';
        $oil_engine_gt = DB::select( $sql_gt, [ $tid ] );
        $oil_engine_gt = !empty( $oil_engine_gt ) ? $oil_engine_gt[0] : '';
        // 康普顿的油 替换成 科发的油
        if( !empty( $oil_engine_gt ) ){
            $sql = 'select coe.name, coe.level, coe.viscosity, coe.id, coe.oil_id,'
                .' koe.level as kefa_level, koe.viscosity as kefa_viscosity, koe.img_url from copton_oil_engine coe '
                . 'left join kefa_oil_engine koe on coe.oil_id=koe.id '
                . ' where coe.`level`=? and coe.viscosity=?';
            $oil_engine = DB::select( $sql, [ $oil_engine_gt->level, $oil_engine_gt->viscosity ] );

            if( !empty( $oil_engine ) ){
                $oil_engine_gt->name = '科发净能';
                $oil_engine_gt->level = $oil_engine[0]->kefa_level;
                $oil_engine_gt->viscosity = $oil_engine[0]->kefa_viscosity;
                //todo 图片链接属性需要添加
                $oil_engine_gt->image = $oil_engine[0]->img_url;
            }
        }
        return $oil_engine_gt;

//        return !empty( $oil_engine_gt ) ? $oil_engine_gt[0] : '';

    }

    public static function oil_nano( $tid ){

        $sql_nano = 'select * from model_oil_engine_nano where model_object_id=?';
        $oil_engine_nano = DB::select( $sql_nano, [ $tid ] );
        return  !empty( $oil_engine_nano ) ? $oil_engine_nano[0] : '';

    }
}